/*-
 * Copyright (c) 2013 Shang-Rong Cai. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $ MTBLAS TEST: tst_lv_a_mat.c    Rev. 0.0.9    2013-11-09T11:52+08:00 $
 */

#include <stdlib.h>

#include <tst_lv_a_com.h>

inline size_t proper_size_mat(size_t max_sz, size_t min_sz)
{
    return max_sz - random() % (max_sz - min_sz);
}


int mat_ff_create(size_t max_sz, size_t min_sz, mat_ff_t *ma)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;

    ma->row_size = proper_size_mat(max_sz, min_sz);
    ma->col_size = proper_size_mat(max_sz, min_sz);
    ma->size = ma->row_size * ma->col_size;

    ma->data = (float*) calloc(ma->size, sizeof(float));

    for (zi = 0; zi < ma->size; zi++)
    {
        ma->data[zi] = random_float();
    }

    return ierr;
}


int mat_ff_delete(mat_ff_t *ma)
{
    int ierr = ERRNO_UNDEFINED;

    free(ma->data);

    return ierr;
}

int mat_ff_twins_create(size_t max_sz, size_t min_sz, mat_ff_t *ma, mat_ff_t *mb)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;

    ma->row_size = proper_size_mat(max_sz, min_sz);
    ma->col_size = proper_size_mat(max_sz, min_sz);
    mb->row_size = ma->row_size;
    mb->col_size = ma->col_size;

    ma->size = ma->row_size * ma->col_size;
    mb->size = ma->size;

    ma->data = (float*) calloc(ma->size, sizeof(float));
    mb->data = (float*) calloc(mb->size, sizeof(float));

    for (zi = 0; zi < ma->size; zi++)
    {
        ma->data[zi] = random_float();
        mb->data[zi] = ma->data[zi];
    }

    return ierr;
}


int mat_ff_twins_reset(mat_ff_t *ma, mat_ff_t *mb)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;

    for (zi = 0; zi < ma->size; zi++)
    {
        mb->data[zi] = ma->data[zi];
    }

    return ierr;
}


int mat_df_create(size_t max_sz, size_t min_sz, mat_df_t *ma)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;

    ma->row_size = proper_size_mat(max_sz, min_sz);
    ma->col_size = proper_size_mat(max_sz, min_sz);
    ma->size = ma->row_size * ma->col_size;

    ma->data = (double*) calloc(ma->size, sizeof(double));

    for (zi = 0; zi < ma->size; zi++)
    {
        ma->data[zi] = random_double();
    }

    return ierr;
}


int mat_df_delete(mat_df_t *ma)
{
    int ierr = ERRNO_UNDEFINED;

    free(ma->data);

    return ierr;
}


int mat_df_twins_create(size_t max_sz, size_t min_sz, mat_df_t *ma, mat_df_t *mb)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;

    ma->row_size = proper_size_mat(max_sz, min_sz);
    ma->col_size = proper_size_mat(max_sz, min_sz);
    mb->row_size = ma->row_size;
    mb->col_size = ma->col_size;

    ma->size = ma->row_size * ma->col_size;
    mb->size = ma->size;

    ma->data = (double*) calloc(ma->size, sizeof(double));
    mb->data = (double*) calloc(mb->size, sizeof(double));

    for (zi = 0; zi < ma->size; zi++)
    {
        ma->data[zi] = random_double();
        mb->data[zi] = ma->data[zi];
    }

    return ierr;
}


int mat_df_twins_reset(mat_df_t *ma, mat_df_t *mb)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;

    for (zi = 0; zi < ma->size; zi++)
    {
        mb->data[zi] = ma->data[zi];
    }

    return ierr;
}


int mat_lf_create(size_t max_sz, size_t min_sz, mat_lf_t *ma)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;

    ma->row_size = proper_size_mat(max_sz, min_sz);
    ma->col_size = proper_size_mat(max_sz, min_sz);
    ma->size = ma->row_size * ma->col_size;

    ma->data = (long double*) calloc(ma->size, sizeof(long double));

    for (zi = 0; zi < ma->size; zi++)
    {
        ma->data[zi] = random_long_double();
    }

    return ierr;
}


int mat_lf_delete(mat_lf_t *ma)
{
    int ierr = ERRNO_UNDEFINED;

    free(ma->data);

    return ierr;
}


int mat_lf_twins_create(size_t max_sz, size_t min_sz, mat_lf_t *ma, mat_lf_t *mb)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;

    ma->row_size = proper_size_mat(max_sz, min_sz);
    ma->col_size = proper_size_mat(max_sz, min_sz);
    mb->row_size = ma->row_size;
    mb->col_size = ma->col_size;

    ma->size = ma->row_size * ma->col_size;
    mb->size = ma->size;

    ma->data = (long double*) calloc(ma->size, sizeof(long double));
    mb->data = (long double*) calloc(mb->size, sizeof(long double));

    for (zi = 0; zi < ma->size; zi++)
    {
        ma->data[zi] = random_long_double();
        mb->data[zi] = ma->data[zi];
    }

    return ierr;
}


int mat_lf_twins_reset(mat_lf_t *ma, mat_lf_t *mb)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;

    for (zi = 0; zi < ma->size; zi++)
    {
        mb->data[zi] = ma->data[zi];
    }

    return ierr;
}


int mat_fc_create(size_t max_sz, size_t min_sz, mat_fc_t *ma)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;

    ma->row_size = proper_size_mat(max_sz, min_sz);
    ma->col_size = proper_size_mat(max_sz, min_sz);
    ma->size = ma->row_size * ma->col_size;

    ma->data = (float complex*) calloc(ma->size, sizeof(float complex));

    for (zi = 0; zi < ma->size; zi++)
    {
        ma->data[zi] = random_float() + _Complex_I * random_float();
    }

    return ierr;
}


int mat_fc_delete(mat_fc_t *ma)
{
    int ierr = ERRNO_UNDEFINED;

    free(ma->data);

    return ierr;
}


int mat_fc_twins_create(size_t max_sz, size_t min_sz, mat_fc_t *ma, mat_fc_t *mb)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;

    ma->row_size = proper_size_mat(max_sz, min_sz);
    ma->col_size = proper_size_mat(max_sz, min_sz);
    mb->row_size = ma->row_size;
    mb->col_size = ma->col_size;

    ma->size = ma->row_size * ma->col_size;
    mb->size = ma->size;

    ma->data = (float complex*) calloc(ma->size, sizeof(float complex));
    mb->data = (float complex*) calloc(mb->size, sizeof(float complex));

    for (zi = 0; zi < ma->size; zi++)
    {
        ma->data[zi] = random_float() + _Complex_I * random_float();
        mb->data[zi] = ma->data[zi];
    }

    return ierr;
}


int mat_fc_twins_reset(mat_fc_t *ma, mat_fc_t *mb)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;

    for (zi = 0; zi < ma->size; zi++)
    {
        mb->data[zi] = ma->data[zi];
    }

    return ierr;
}


int mat_dc_create(size_t max_sz, size_t min_sz, mat_dc_t *ma)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;

    ma->row_size = proper_size_mat(max_sz, min_sz);
    ma->col_size = proper_size_mat(max_sz, min_sz);
    ma->size = ma->row_size * ma->col_size;

    ma->data = (double complex*) calloc(ma->size, sizeof(double complex));

    for (zi = 0; zi < ma->size; zi++)
    {
        ma->data[zi] = random_double() + _Complex_I * random_double();
    }

    return ierr;
}


int mat_dc_delete(mat_dc_t *ma)
{
    int ierr = ERRNO_UNDEFINED;

    free(ma->data);

    return ierr;
}


int mat_dc_twins_create(size_t max_sz, size_t min_sz, mat_dc_t *ma, mat_dc_t *mb)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;

    ma->row_size = proper_size_mat(max_sz, min_sz);
    ma->col_size = proper_size_mat(max_sz, min_sz);
    mb->row_size = ma->row_size;
    mb->col_size = ma->col_size;

    ma->size = ma->row_size * ma->col_size;
    mb->size = ma->size;

    ma->data = (double complex*) calloc(ma->size, sizeof(double complex));
    mb->data = (double complex*) calloc(mb->size, sizeof(double complex));

    for (zi = 0; zi < ma->size; zi++)
    {
        ma->data[zi] = random_double() + _Complex_I * random_double();
        mb->data[zi] = ma->data[zi];
    }

    return ierr;
}


int mat_dc_twins_reset(mat_dc_t *ma, mat_dc_t *mb)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;

    for (zi = 0; zi < ma->size; zi++)
    {
        mb->data[zi] = ma->data[zi];
    }

    return ierr;
}


int mat_lc_create(size_t max_sz, size_t min_sz, mat_lc_t *ma)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;

    ma->row_size = proper_size_mat(max_sz, min_sz);
    ma->col_size = proper_size_mat(max_sz, min_sz);
    ma->size = ma->row_size * ma->col_size;

    ma->data = (long double complex*) calloc(ma->size, sizeof(long double complex));

    for (zi = 0; zi < ma->size; zi++)
    {
        ma->data[zi] = random_long_double() + _Complex_I * random_long_double();
    }

    return ierr;
}


int mat_lc_delete(mat_lc_t *ma)
{
    int ierr = ERRNO_UNDEFINED;

    free(ma->data);

    return ierr;
}


int mat_lc_twins_create(size_t max_sz, size_t min_sz, mat_lc_t *ma, mat_lc_t *mb)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;

    ma->row_size = proper_size_mat(max_sz, min_sz);
    ma->col_size = proper_size_mat(max_sz, min_sz);
    mb->row_size = ma->row_size;
    mb->col_size = ma->col_size;

    ma->size = ma->row_size * ma->col_size;
    mb->size = ma->size;

    ma->data = (long double complex*) calloc(ma->size, sizeof(long double complex));
    mb->data = (long double complex*) calloc(mb->size, sizeof(long double complex));

    for (zi = 0; zi < ma->size; zi++)
    {
        ma->data[zi] = random_long_double() + _Complex_I * random_long_double();
        mb->data[zi] = ma->data[zi];
    }

    return ierr;
}


int mat_lc_twins_reset(mat_lc_t *ma, mat_lc_t *mb)
{
    int ierr = ERRNO_UNDEFINED;

    size_t zi;

    for (zi = 0; zi < ma->size; zi++)
    {
        mb->data[zi] = ma->data[zi];
    }

    return ierr;
}
